<template>
  <section>
    <!--搜索筛选-->
    <el-form label-width="80px" inline style="border: 0px solid red">
      <el-form-item label="" style="border: 0px solid red;">
        <el-row style="display: flex;flex-direction: row;width: 100%">
          <filter-tool class="filtertool" type="enabled" test="name" v-on:confirm="filterEvent"></filter-tool>
        </el-row>
      </el-form-item>
      <el-form-item label="" style="border: 0px solid red;">
        <search-tool v-on:search="searchEvent"></search-tool>
      </el-form-item>
      <el-form-item label="" style="border: 0px solid red;float: right">
        <el-button size="mini" @click="addBtn" type="primary" icon="el-icon-plus"/>
      </el-form-item>
    </el-form>
    <!--列表-->
    <el-table :data="itemList">
      <el-table-column type="index" label="序号"/>
      <el-table-column label="标题" prop="name"/>
      <el-table-column label="编码" prop="code">
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="primary" @click='editBtn(scope.row)' size="mini">编辑</el-button>
          <el-button @click='delBtn(scope)' size="mini" type="danger">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--对话框-->
    <el-dialog
      :title="currItem.id?'编辑':'新建'"
      :visible.sync="showDialog" v-if="currItem"
      width="80%"
      :before-close="getList">
    <!--<el-dialog :title="currItem.id?'编辑':'新建'" :visible.sync="showDialog"-->
               <!--width="80%" :close-on-click-modal="false"  v-if="showDialog" :before-close="test(filterObj)">-->
      <edit-examine-type :typeForm="currItem" v-on:success="getList(filterObj)"></edit-examine-type>
    </el-dialog>
  </section>
</template>

<script>
  import editExamineType from './editExamineType'
  import searchTool from '@/common/searchTool'
  import filterTool from '@/common/filterTool'
  import {mapGetters, mapActions, mapMutations} from 'vuex'
  import Vue from 'vue'//后续添加属性，需要vue联动
  import errHandler from '@/assets/errHandler'
  export default {
    name: "index",
    components:{filterTool,searchTool,editExamineType},
    data(){
      return{
        currItem:null,
        itemList:null,
        showDialog:false,
        filterObj:{}
      }
    },
    created(){
      this.getList()
    },
    computed:{
      ...mapGetters(['department']),
    },
    watch: {
    },
    methods: {
      ...mapActions({
        fetch: 'fetch'
      }),
      test(){
        console.log('为啥又循环-------')
        this.getList()
      },
      getList(filterObj){
        // console.log('为啥又循环-------')
        this.fetch({name:"getExamineTypeList",data:{departmentId:this.department.id,...filterObj}})
          .then(res=>{
            console.log('---------------',res)
            this.itemList=res
            this.showDialog=false
          })
          .catch(err=>{
            console.log(err)
            errHandler.handle(this,err)
          })
      },
      filterEvent(e,objK){
        for(let key in e){
          if(objK === undefined) this.filterObj[key]=e[key]
          else this.filterObj[objK]=e[key]
        }
        console.log('看下filterObj----------',this.filterObj)
        let tmp={}
        for(let k in this.filterObj){
          if(this.filterObj[k] !== undefined){
            tmp[k]=this.filterObj[k]
          }
        }
        this.getList(tmp)//筛选入库单列表
      },
      searchEvent(e){
        this.filterObj.key=e
        this.getList(this.filterObj)
      },
      addBtn(){
        this.currItem={name:'',code:''}
        this.showDialog=true
      },
      editBtn(row){
        this.currItem=row
        this.showDialog=true
      },
      delBtn(scope){
        this.$confirm('确认删除？')
          .then(()=> {
            this.fetch({name:"delExamineType",data:{id:scope.row.id}})
              .then(res=>{
                this.getList(this.filterObj)
              })
              .catch(err=>{
                console.log(err)
                errHandler.handle(this,err)
              })
          })
          .catch(() => {
            this.$message({
              type: 'info',
              message: '已取消删除'
            })
          })
      },
    }
  }
</script>

<style scoped>

</style>
